import 'package:flutter/material.dart';
import 'package:flutter_syt/api/api.dart';
import 'package:flutter_syt/datas/venue_item.dart';
import 'package:flutter_syt/domain/games/game_utils/game_utils_controller.dart';
import 'package:flutter_syt/domain/games/venue_item.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_syt/routes.all.dart';
import 'package:flutter_syt/utils/click_utils.dart';
import 'package:flutter_syt/widgets/normal_table.dart';
import 'package:yyy_route_annotation/yyy_route_annotation.dart';

@RoutePage(name: "venueList")
class VenuePage extends StatefulWidget {
  final bool needTitle = true;
  @override
  State<StatefulWidget> createState() {
    return _VenuePageState();
  }
}

class VenueList extends VenuePage {
  @override
  bool get needTitle => false;
  @override
  State<StatefulWidget> createState() {
    return _VenuePageState();
  }
}

class _VenuePageState extends State<VenuePage> {
  // GameUtilsController
  NormalTableViewController controller = NormalTableViewController();
  @override
  Widget build(BuildContext context) {
    var body = NormalTableView<VenueItem>(
      enableRefresh: true,
      enableLoadMore: false,
      controller: controller,
      padding: EdgeInsets.symmetric(horizontal: 15, vertical: 20),
      itemBuilder: (ctx, item) {
        var entity = VenueEntity(item.cover, item.name, item.address);
        return GestureDetector(
            child: Container(
              child: VenueItemCell(entity),
              padding: EdgeInsets.only(bottom: 10),
            ),
            onTap: ClickUtils.debounce(
              () => RouteManager.push(
                context,
                RoutMapping.venueDetailPage(item.id),
              ),
            ));
      },
      getData: (page) async {
        var json = await API.shared.venue.list();
        // List list = [];
        // json.forEach((element) {
        //   list.add(element.cover);
        // });
        // DLog(list);

        return json;
      },
    );
    if (widget.needTitle) {
      return Scaffold(
        appBar: AppBar(
          title: Text(S.of(context).venue),
        ),
        body: body,
      );
    } else {
      return body;
    }
  }

  @override
  void initState() {
    super.initState();
    GameUtilsController.managerCont['5'] = controller;
  }
}
